iT邦幫忙

1

資料結構與演算法[2]

  • 分享至 

  • xImage
  •  

繼上篇,先把這些容器的基本語法學起來

跟上一篇同樣的圖 :
https://ithelp.ithome.com.tw/upload/images/20210630/20114067B1zuNm9TSk.png

https://ithelp.ithome.com.tw/upload/images/20210630/20114067ahjrUmR11Q.png

Dictionary

程式碼 :

Dictionary<int, string> dictionary = new Dictionary<int, string>();
dictionary.Add(2, "AA");
dictionary.Add(23, "BB");
dictionary.Add(5, "CC");
dictionary.Add(1, "DD");

foreach (var item in dictionary)
{
    Console.WriteLine($"key = {item.Key} , value = {item.Value}");
}

執行結果 :

https://ithelp.ithome.com.tw/upload/images/20210702/20114067bTV8aplfE4.png

SortDictionary

程式碼 :

SortedDictionary<int, string> sortedDictionary
    = new SortedDictionary<int, string>();
sortedDictionary.Add(2, "AA");
sortedDictionary.Add(23, "BB");
sortedDictionary.Add(5, "CC");
sortedDictionary.Add(1, "DD");

foreach (var item in sortedDictionary)
{
    Console.WriteLine($"key = {item.Key} , value = {item.Value}");
}

執行結果 :

https://ithelp.ithome.com.tw/upload/images/20210702/20114067GmoACod80h.png

如果要對類別做排列的話 :

程式碼 :

internal class Person
{
    public int Age { get; set; }
    public string Name { get; set; }
}

實作ICompare並定義排序條件

internal class PersonComparer : IComparer<Person>
{
    public int Compare(Person p1, Person p2)
    {
        int result;
        result = p1.Age.CompareTo(p2.Age);

        if (result == 0)
            result = p1.Name.CompareTo(p2.Name);

        return result;
    }
}

宣告時放入PersonComparer

SortedDictionary<Person, string> sortedDictionary2
    = new SortedDictionary<Person, string>(new PersonComparer());
sortedDictionary2.Add(new Person { Name = "BB", Age = 6 }, "BB2");
sortedDictionary2.Add(new Person { Name = "AA", Age = 7 }, "AA2");
sortedDictionary2.Add(new Person { Name = "A", Age = 3 }, "A2");
sortedDictionary2.Add(new Person { Name = "CCC", Age = 30 }, "CCC2");
foreach (var item in sortedDictionary2)
{
    Console.WriteLine($"key = {item.Key.Name}-{item.Key.Age} 
	, value = {item.Value}");
}

執行結果 :

https://ithelp.ithome.com.tw/upload/images/20210702/20114067Tsgktrg4RZ.png

List

程式碼 :

List<int> list = new List<int>();
list.Add(2);
list.Add(23);
list.Add(5);
list.Add(1);

foreach (var item in list)
{
    Console.WriteLine($"value = {item}");
}

執行結果 :

https://ithelp.ithome.com.tw/upload/images/20210702/20114067n9r2IxfdrK.png

SortedList

程式碼 :

SortedList<int, string> sortList = new SortedList<int, string>();
sortList.Add(2, "AA");
sortList.Add(23, "BB");
sortList.Add(5, "CC");
sortList.Add(1, "DD");

foreach (var item in sortList)
{
    Console.WriteLine($"key = {item.Key} , value = {item.Value}");
}

執行結果 :

https://ithelp.ithome.com.tw/upload/images/20210702/201140672lOak4GJ4r.png

HashSet

程式碼 :

HashSet<int> hashSet = new HashSet<int>
{
    2,23,5,1
};

foreach (var item in hashSet)
{
    Console.WriteLine($"value = {item}");
}

執行結果 :

https://ithelp.ithome.com.tw/upload/images/20210702/20114067OLwfaRbGkU.png

SortedSet

程式碼 :

SortedSet<int> sortedSet = new SortedSet<int>();
sortedSet.Add(2);
sortedSet.Add(23);
sortedSet.Add(5);
sortedSet.Add(1);

foreach (var item in sortedSet)
{
    Console.WriteLine($"value = {item}");
}

執行結果 :

https://ithelp.ithome.com.tw/upload/images/20210702/20114067yQM49ILSoK.png

Stack

程式碼 :

Stack<int> stack = new Stack<int>();
stack.Push(9);
stack.Push(78);
stack.Push(66);
stack.Push(55);

Console.WriteLine($"Peek = {stack.Peek()}");

while (stack.Count() != 0)
{
    Console.WriteLine($"value = {stack.Pop()}");
}

執行結果 :

https://ithelp.ithome.com.tw/upload/images/20210702/20114067DaaETVM7Ix.png

Queue

程式碼 :

Queue<int> queue = new Queue<int>();
queue.Enqueue(9);
queue.Enqueue(78);
queue.Enqueue(66);
queue.Enqueue(55);

while (queue.Count() != 0)
{
    Console.WriteLine($"value = {queue.Dequeue()}");
}

執行結果 :

https://ithelp.ithome.com.tw/upload/images/20210702/201140675eID9lc4T4.png

LinkedList

程式碼 :

LinkedList<int> linkList = new LinkedList<int>();
linkList.AddFirst(1);
linkList.AddFirst(2);
linkList.AddFirst(3);
linkList.AddLast(7);
linkList.AddLast(8);
linkList.AddLast(9);

linkList.RemoveFirst();
linkList.RemoveLast();

foreach (var item in linkList)
{
    Console.WriteLine($"value = {item}");
}

執行結果 :

https://ithelp.ithome.com.tw/upload/images/20210702/20114067xbDsznliKv.png
熟悉基本語法後再找適當的演算法放不同容器試一試
https://ithelp.ithome.com.tw/upload/images/20210702/20114067IPNoyfQSeg.png

新手發文,若有錯誤的地方請不吝色的指正,謝謝。


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言